package com.scau.action.business;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ModelDriven;
import com.scau.action.BaseAction;
import com.scau.exception.BusinessException;
import com.scau.model.business.ModelRule;
import com.scau.service.impl.business.ModelRuleService;
import com.scau.util.PageController;


@Component
@Scope("prototype")
public class ModelRuleAction extends BaseAction implements Serializable,ModelDriven<ModelRule> {
	private static final long serialVersionUID = 8299975587235537983L;
	private final static Log logger = LogFactory.getLog(ModelRuleAction.class);
	private PageController pager = null;
	private ModelRuleService modelRuleService = null;
	private ModelRule modelRule = null;
	
	
	/**
	 * 列表显示
	 * @return
	 */
	public String list() {
		int totalRows = modelRuleService
				.getRecordCount(new ModelRule());
		String URL =getListURL();
		this.pager.setURL(URL);
		this.pager.setTotalRowsAmount(totalRows);
		List<ModelRule> resourceList = modelRuleService.list(
				new ModelRule(), this.pager.getPageStartRow(),
				pager.getPageSize(), null,null);
		pager.setData(resourceList);
		return "list";
	}

	/**
	 * 修改
	 * @return
	 */
	public String get() {
		modelRule = modelRuleService.get(modelRule);
		return "edit";
	}
	
	/**
	 * 保存
	 * @return
	 */
	public String save() {
		try {
			String waterproof = ("小".equals(request.getParameter("waterproof1")) ? "" : request
					.getParameter("waterproof1"))
					+ "/"
					+ ("中".equals(request.getParameter("waterproof2")) ? ""
							: request.getParameter("waterproof2"))
					+ "/"
					+ ("大".equals(request.getParameter("waterproof3")) ? ""
							: request.getParameter("waterproof3"));
			String innerheight = ("小".equals(request
					.getParameter("innerheight1")) ? "" : request
					.getParameter("innerheight1"))
					+ "/"
					+ ("中".equals(request.getParameter("innerheight2")) ? ""
							: request.getParameter("innerheight2"))
					+ "/"
					+ ("大".equals(request.getParameter("innerheight3")) ? ""
							: request.getParameter("innerheight3"));
			String heel = ("小".equals(request.getParameter("heel1")) ? ""
					: request.getParameter("heel1"))
					+ "/"
					+ ("中".equals(request.getParameter("heel2")) ? ""
							: request.getParameter("heel2"))
					+ "/"
					+ ("大".equals(request.getParameter("heel3")) ? ""
							: request.getParameter("heel3"));
			modelRule.setWaterproof(waterproof);
			modelRule.setInnerheight(innerheight);
			modelRule.setHeel(heel);
			modelRuleService.save(modelRule);
			return "list";
		} catch (BusinessException e) {
			// 保存原来表单已输入的内容
			request.setAttribute("modelRule", modelRule);
			request.setAttribute("message", e.getMessage());
			return "edit";
		}
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String del() {
		String[] ids = request.getParameterValues("id");
		for (String id : ids) {
			ModelRule modelRule = new ModelRule();
			if (null != id && !("".equals(id))) {
				modelRule.setId(Long.parseLong(id));
				modelRuleService.delete(modelRule);
			}
		}
		return "list";
	}


	public PageController getPager() {
		return pager;
	}

	@Resource
	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public ModelRuleService getModelRuleService() {
		return modelRuleService;
	}

	@Resource
	public void setModelRuleService(ModelRuleService modelRuleService) {
		this.modelRuleService = modelRuleService;
	}

	public ModelRule getModelRule() {
		return modelRule;
	}

	public void setModelRule(ModelRule modelRule) {
		this.modelRule = modelRule;
	}

	@Override
	public ModelRule getModel() {
		// TODO Auto-generated method stub
		return modelRule;
	}

}
